package com.jck.wx.util;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;


/**
 * idcard 工具类
 * @author Administrator
 *
 */
public class IdcardUtil {
		public static String getJsonString (String WEBOCR_URL,String ENGINE_TYPE,String HEAD_PORTRAIT,String ID_NUMBER_IMAGE,String APPID,String  API_KEY,byte[] imageByte ) {
			String result=null;
			try {
				Map<String, String> header = buildHttpHeader(ENGINE_TYPE, HEAD_PORTRAIT, ID_NUMBER_IMAGE, APPID, API_KEY);
				String imageBase64 = new String(Base64.encodeBase64(imageByte), "UTF-8");
				 result = HttpUtil.sendPost(WEBOCR_URL, header, "image=" + URLEncoder.encode(imageBase64, "UTF-8"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return result;
		}
		public static String getJsonString (String WEBOCR_URL,String ENGINE_TYPE,String HEAD_PORTRAIT,String ID_NUMBER_IMAGE,String APPID,String  API_KEY,String imageBase64,boolean flag ) {
			String result=null;
			try {
				Map<String, String> header = buildHttpHeader(ENGINE_TYPE, HEAD_PORTRAIT, ID_NUMBER_IMAGE, APPID, API_KEY);
				 result = HttpUtil.sendPost(WEBOCR_URL, header, "image=" + URLEncoder.encode(imageBase64, "UTF-8"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return result;
		}
		public static String getJsonString (String WEBOCR_URL,String ENGINE_TYPE,String HEAD_PORTRAIT,String ID_NUMBER_IMAGE,String APPID,String  API_KEY,String IMAGE_PATH ) {
			String result=null;
			try {
				Map<String, String> header = buildHttpHeader(ENGINE_TYPE, HEAD_PORTRAIT, ID_NUMBER_IMAGE, APPID, API_KEY);
				byte[] imageByteArray = FileUtil.read(IMAGE_PATH);
				String imageBase64 = new String(Base64.encodeBase64(imageByteArray), "UTF-8");
				 result = HttpUtil.sendPost(WEBOCR_URL, header, "image=" + URLEncoder.encode(imageBase64, "UTF-8"));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return result;
		}

		/**
		 * 组装http请求头
		 */
		private static Map<String, String> buildHttpHeader(String ENGINE_TYPE,String HEAD_PORTRAIT,String ID_NUMBER_IMAGE,String APPID,String  API_KEY ) throws UnsupportedEncodingException {
			String curTime = System.currentTimeMillis() / 1000L + "";
			String param = "{\"engine_type\":\"" + ENGINE_TYPE + "\",\"head_portrait\":\"" + HEAD_PORTRAIT + "\",\"id_number_image\":\"" + ID_NUMBER_IMAGE +"\"}";
			String paramBase64 = new String(Base64.encodeBase64(param.getBytes("UTF-8")));
			String checkSum = DigestUtils.md5Hex(API_KEY + curTime + paramBase64);
			Map<String, String> header = new HashMap<String, String>();
			header.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
			header.put("X-Param", paramBase64);
			header.put("X-CurTime", curTime);
			header.put("X-CheckSum", checkSum);
			header.put("X-Appid", APPID);
			return header;
		}
}